package com.soft.hwx.scdapp.main;

import android.util.Log;
import android.widget.TextView;

import com.soft.hwx.logutillib.LogUtil;
import com.soft.hwx.scdapp.MainActivity;
import com.soft.hwx.scdapp.msg.HostRealseSearchRspMsg;
import com.soft.hwx.scdapp.msg.HostSearchRspMsg;
import com.soft.hwx.scdapp.msg.HostSelfCheckRspMsg;
import com.soft.hwx.scdapp.msg.HostSetArgsRspMsg;
import com.soft.hwx.scdapp.msg.HostWorkStatus;
import com.soft.hwx.scdapp.msg.RawMsg;
import com.soft.hwx.scdapp.socket.UdpHelper;
import com.soft.hwx.scdapp.socket.UdpRecvInterface;

import java.text.SimpleDateFormat;
import java.util.LinkedList;


/*
    侦察主机接收线程类

 */
public class HostRecvThread implements Runnable,UdpRecvInterface{
    public    Boolean IsThreadDisable = false;//指示监听线程是否终止


    private   UdpHelper udpServer;
    private   AppConfig  config;
    private   Thread updListenThread;
    private   ScoutHostInterface mScoutHostInterface;

    //先入先出
    private  LinkedList<RawMsg> msgList = new LinkedList();
    private  Object msgListLock = new Object();

    public HostRecvThread( ScoutHostInterface mScoutHostInterface){
        this.mScoutHostInterface = mScoutHostInterface;
    }

    /*
    可以作为FIFO(先进先出)的队列，作为FIFO的队列时，下表的方法等价：
            队列方法       等效方法
        add(e)        addLast(e)
        offer(e)      offerLast(e)
        remove()      removeFirst()
        poll()        pollFirst()
        element()     getFirst()
        peek()        peekFirst()
     */


    public void AddRecvMsg(byte[] buf,int len){
        RawMsg msg = new RawMsg(buf,len);

        //临界区  保护队列
        synchronized(msgListLock) {
            msgList.addLast(msg);
        }
    }

    public  int StartControl(){
        //读取配置 监听的端口  主机的ip  等信息
        config = new AppConfig();
        if(config.LoadConfig()!=0){
           Log.d("HostRecvThread", "LoadConfig faild");
           return -1;
        }

        //创建UDP 服务器 监听消息
        udpServer = new UdpHelper(config.getListenLocalPort(),this);
        updListenThread = new Thread(udpServer);
        try {
            updListenThread.start();
        }catch (Exception e){
            Log.d("HostRecvThread", "创建UDP 监听线程失败:"+e.getMessage());
            return -1;
        }

        return 0;
    }

    private void procMsgPool(){

        while(!IsThreadDisable){
            if(msgList.size()<=0){
                try {
                    //主线程等待通过notify() 唤醒
                    //临界区  保护队列
                    synchronized(msgListLock) {
                            msgListLock.wait();
                    }
                }catch (Exception e){

                }
            }

            if(msgList.size()<=0){
                continue;
            }

            RawMsg msg = null;
            //临界区  保护队列
            synchronized(msgListLock) {
                msg = msgList.getFirst();
                msgList.removeFirst();
            }

            if(msg==null)
                continue;

            //解析消息
            if(msg.ParseHeader()!=0){
                continue;
            }

            //根据工作结果字  判断 报文类型
            /*
            switch (msg.workChar){
                case HostWorkResult.selfCheckDoing:
                case HostWorkResult.selfCheckWell:
                case HostWorkResult.selfCheckError:
                    HostSelfCheckRspMsg selfCheckMsg = new HostSelfCheckRspMsg(msg);
                    break;
                case HostWorkResult.setDoing:
                case HostWorkResult.setWell:
                case HostWorkResult.setError:
                    HostSetArgsRspMsg setArgsRspMsg = new HostSetArgsRspMsg(msg);
                    break;
                case HostWorkResult.searchDoing:
                case HostWorkResult.searchTracking:
                case HostWorkResult.searchLost:
                    HostSearchRspMsg searchRspMsg = new HostSearchRspMsg(msg);
                    break;

                case HostWorkResult.releaseSearchSuccess:
                    break;
                default:
                    break;
            }*/

            //或者根据报文工作状态 判断报文类型
            switch (msg.workStatus){
                case HostWorkStatus.selfCheck:
                    //LogUtil.d("HostWorkStatus.selfCheck!!");
                    HostSelfCheckRspMsg selfCheckMsg = new HostSelfCheckRspMsg(msg);
                    mScoutHostInterface.showMes(selfCheckMsg);
                    //showHostSelfCheckRspMsgUI(selfCheckMsg);
                    break;
                case HostWorkStatus.setArgs:
                    HostSetArgsRspMsg setArgsRspMsg = new HostSetArgsRspMsg(msg);
                    //比较装订信息,判断是否装订成功
                    //
                    mScoutHostInterface.showMes(setArgsRspMsg);
                    //showHostSetArgsRspMsgUI(setArgsRspMsg);
                    break;
                case HostWorkStatus.search:
                    HostSearchRspMsg searchRspMsg = new HostSearchRspMsg(msg);

                    mScoutHostInterface.showMes(searchRspMsg);
                    //showHostSearchRspMsgUI(searchRspMsg);
                    break;
                case HostWorkStatus.releaseSearch:
                    HostRealseSearchRspMsg RealseSearchRspMsg = new HostRealseSearchRspMsg(msg);
                    mScoutHostInterface.showMes(RealseSearchRspMsg);
                    break;
            }
            //送去界面显示
            //mScoutHostInterface.showMes(msg);


        }
    }
    /*
            UDP recv callback

     */
    @Override
    public void  Recv(byte[] buf,int len)
    {
        //放入队列 等待处理
        //Log.d("HostRecvThread", "AddRecvMsg");
        AddRecvMsg(buf,len);

        //外部线程调用
        //唤醒当前的wait线程
        synchronized(msgListLock) {
            msgListLock.notify();
        }
    }
    /*
       启动线程 处理接收到的消息并显示到桌面
       判断是否有消息需要发送
     */
    @Override
    public void run() {
        //StartListen();
        StartControl();
        procMsgPool();
    }
}
